using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using System.IO.Ports;
using System.Text;

#if MINI
using SecretLabs.NETMF.Hardware.NetduinoMini;
#else
using SecretLabs.NETMF.Hardware.Netduino;
#endif

namespace codebase
{
    public class windowCalculator : UI.Window
    {
        UI.Button[] calcButtons;
        UI.Label calcResult;

        public windowCalculator(UI.WindowManager win, int x, int y)
        {
            Initialize(win, x, y);
        }

        public override void Initialize(UI.WindowManager win, int x, int y)
        {
            base.Initialize(win, 14, 17);

            Left = x * screen.CharacterWidth;
            Top = y * screen.CharacterHeight;
            CaptionText = "Calculator";

            calcButtons = new UI.Button[15];
            calcButtons[0] = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 5);
            calcButtons[0].Text = "\n 7 \n";
            calcButtons[1] = (UI.Button)Add(UI.Window.ControlTypes.Button, 4, 5);
            calcButtons[1].Text = "\n 8 \n";
            calcButtons[2] = (UI.Button)Add(UI.Window.ControlTypes.Button, 7, 5);
            calcButtons[2].Text = "\n 9 \n";

            calcButtons[3] = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 8);
            calcButtons[3].Text = "\n 4 \n";
            calcButtons[4] = (UI.Button)Add(UI.Window.ControlTypes.Button, 4, 8);
            calcButtons[4].Text = "\n 5 \n";
            calcButtons[5] = (UI.Button)Add(UI.Window.ControlTypes.Button, 7, 8);
            calcButtons[5].Text = "\n 6 \n";

            calcButtons[6] = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 11);
            calcButtons[6].Text = "\n 1 \n";
            calcButtons[7] = (UI.Button)Add(UI.Window.ControlTypes.Button, 4, 11);
            calcButtons[7].Text = "\n 2 \n";
            calcButtons[8] = (UI.Button)Add(UI.Window.ControlTypes.Button, 7, 11);
            calcButtons[8].Text = "\n 3 \n";

            calcButtons[9] = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 2);
            calcButtons[9].Text = "\n + \n";
            calcButtons[10] = (UI.Button)Add(UI.Window.ControlTypes.Button, 4, 2);
            calcButtons[10].Text = "\n - \n";
            calcButtons[11] = (UI.Button)Add(UI.Window.ControlTypes.Button, 7, 2);
            calcButtons[11].Text = "\n * \n";
            calcButtons[12] = (UI.Button)Add(UI.Window.ControlTypes.Button, 10, 2);
            calcButtons[12].Text = "\n / \n";

            calcButtons[13] = (UI.Button)Add(UI.Window.ControlTypes.Button, 10, 8);
            calcButtons[13].Text = "\n = \n";

            calcButtons[14] = (UI.Button)Add(UI.Window.ControlTypes.Button, 10, 11);
            calcButtons[14].Text = "\n 0 \n";

            foreach (UI.Button btn in calcButtons)
            {
                btn.Pressed += new UI.Button.ButtonPressedDelegate(calc_Pressed);
                btn.NotPressedColor = ColorExtensions.RGB16(60, 220, 60);
                btn.PressedColor = ColorExtensions.RGB16(60, 60, 220);
            }

            calcResult = (UI.Label)Add(UI.Window.ControlTypes.Label, 1, 15);
            calcResult.Text = "";
            calcResult.BackgroundColor = ColorExtensions.RGB16(80, 220, 80);
        }

        void calc_Pressed(UI.Button btn)
        {
            if (btn.Text.IndexOf('=') <= 0)
            {
                string txt = btn.Text.Replace("\n", "");
                txt = txt.Replace(" ", "");
                calcResult.Text += txt;
                calcResult.Refresh();
            }
            else
            {
                double res = ExpressionSolver.EvalExpression(calcResult.Text.ToCharArray());
                string _res = res.ToString();

                if (_res.Length > 10) _res = _res.Substring(0, 10);

                calcResult.Text = "";
                Redraw();
                calcResult.Text = _res;
                calcResult.Refresh();
                Thread.Sleep(2000);
                calcResult.Text = "";

                Redraw();
            }
        }
    }
}
